package com.xust.oa.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xust.oa.domain.dto.DepartmentDTO;
import com.xust.oa.domain.dto.PositionDTO;
import com.xust.oa.domain.entity.TDepartment;
import com.xust.oa.domain.entity.TPosition;
import com.xust.oa.domain.vo.DepartmentVO;
import com.xust.oa.service.TPositionService;
import com.xust.oa.utils.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zj
 * @time 2025/3/26 9:52
 */
@RestController
@RequestMapping("/position")
public class PositionController {

    @Autowired
    private TPositionService positionService;


    @GetMapping("/count")
    public Result<Long> getCount(PositionDTO positionDTO){

        QueryWrapper<TPosition> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(positionDTO.getName()))
            queryWrapper.like("name", positionDTO.getName());
        if (!StringUtils.isEmpty(positionDTO.getEnabled()))
            queryWrapper.eq("enabled", positionDTO.getEnabled());
        long count = positionService.count(queryWrapper);
        return Result.ok(count);
    }
    @PostMapping("/getList")
    public Result<List<TPosition>> getDeptList(@RequestBody PositionDTO positionDTO){
        return positionService.searchList(positionDTO);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        try {
            positionService.removeById(id);
            return Result.ok("删除成功");
        }catch (Exception e){
            return Result.buildFailure(400,"删除失败");
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody PositionDTO positionDTO){
        if (positionDTO== null||positionDTO.getName()==null) return Result.buildFailure(400,"参数错误");
        positionDTO.setId(null);
        try {
            TPosition position = new TPosition();
            BeanUtils.copyProperties(positionDTO,position);

            position.setCreateDate(new Date(System.currentTimeMillis()));
            positionService.save(position);
            return Result.ok("添加成功");
        }catch (Exception e){
            return Result.buildFailure(400,"添加失败,请检查部门名参数是否重复");
        }
    }

    @GetMapping("/list")
    public Result<List<String>> getAllPosition(){
        List<String> list = positionService.list().stream().map(TPosition::getName).collect(Collectors.toList());
        return Result.ok(list);
    }
    @PostMapping("/update")
    public Result update(@RequestBody PositionDTO positionDTO){
        if (positionDTO == null||positionDTO.getId()==null) return Result.buildFailure(400,"id不能为空");

        try {
            TPosition position = new TPosition();
            BeanUtils.copyProperties(positionDTO,position);
            positionService.updateById(position);
            return Result.ok("修改成功");
        }catch (Exception e){
            return Result.exception(403,"修改失败,请检查职位名参数是否重复");
        }
    }
}
